<?php
    $auth = Auth::getAuth('current_user');
    $db = new Database();
    $userCompany = new userQueries();
    // header('Content-Type: application/json');
    $action = $_POST['action'];
    
        if(isset($action)){
            
             //Load Table data
            
            switch($action){
                case "companyUserActive":
                    $res = "";
                    $out = "";
                    $fields_out = "";
                    $company_user_info = array();
                    $fields = array(); 
                    
                    //$companyUsers_fields = $db->query("SHOW COLUMNS FROM tbuser");
                    $company = $userCompany->getCompany($auth['company_id']);
                    $companyUsers = $db->query("SELECT u.*,o.department as depName
                                               FROM tbuser u
                                               LEFT JOIN tborgchartobjects o on o.id=u.department_id
                                               WHERE u.company_id={$db->escape($auth['company_id'])}
                                                    AND u.is_active='1'
                                                ORDER BY u.date_registered DESC","array");
                    $countRows = $db->query("SELECT u.*,o.department as depName
                                               FROM tbuser u
                                               LEFT JOIN tborgchartobjects o on o.id=u.department_id
                                               WHERE u.company_id={$db->escape($auth['company_id'])}
                                                    AND u.is_active='1'","numrows");
                    // Table fields
                        //foreach($companyUsers as $info){
                        //    $company_user_info[] = $info;
                        //}
                        
                        
                        foreach($companyUsers as $info){
                            // Actions
                            $actions = '<center><div class="icon-edit fl-margin-right fa fa-pencil cursor editUserListInfo" id="editUserListInfo_'.$info['id'].'" data-type="deactivate" data-user-id="'.$info['id'].'"></div>'.
                            '<div class="icon-trash fa fa-trash-o cursor deleteUserListInfo" id="deleteUser_'.$info['id'].'" data-type="deactivate" data-user-id="'.$info['id'].'"></div></center>';
                              
                            // $checkbox = '<center><input type="checkbox" data-id="'.$info['id'].'" name="selectCheckBox[]" class="selectCheckBox" ></center>';
                              
                            $company_user_info[] = array(
                                                        "userCheckbox"=>$checkbox,
                                                        "userID"=>$info['id'],
                                                        "image"=>$userCompany->avatarPic("tbuser",$info['id'],"30","30","small","avatar"),
                                                        "email"=>$info['email'],
                                                        "displayName"=>$info['display_name'],
                                                        "fullname"=>$info['first_name']. " " .$info['last_name'],
                                                        "position"=>$info['position'],
                                                        "department"=>$info['depName'],
                                                        "dateRegistered"=>$info['date_registered'],
                                                        "actions"=>$actions);
                            
                        }
                            $res = array(/*"sEcho"                        =>      "3",
                                         "iTotalRecords"                =>      $countRows,
                                         "iTotalDisplayRecords"         =>      $countRows,*/
                                         "company_user"                 =>      $company_user_info);
                        
                            $string = json_encode($res);
                            
                            echo functions::view_json_formatter($string);
                        break;
                
                case "companyUserActiveDatatable":
                    $res = "";
                    $company_user_info = array();
                    $start = $_POST['iDisplayStart'];
                    $company = $userCompany->getCompany($auth['company_id']);
                    $search = $_POST['search_value'];
                    $orderBy = " ORDER BY u.date_registered DESC";
                    if($_POST['column-sort']){
                      $orderBy = " ORDER BY ".$_POST['column-sort']." ".$_POST['column-sort-type'];
                    }
                    $companyUsers = $db->query("SELECT u.*,o.department as depName
                                               FROM tbuser u
                                               LEFT JOIN tborgchartobjects o on o.id=u.department_id
                                               WHERE u.company_id={$db->escape($auth['company_id'])}
                                                    AND u.is_active='1' AND (u.email LIKE '%$search%' || u.first_name LIKE '%$search%' || u.last_name LIKE '%$search%' || u.position LIKE '%$search%')
                                                $orderBy LIMIT $start,10","array");
                    $countRows = $db->query("SELECT u.*,o.department as depName
                                               FROM tbuser u
                                               LEFT JOIN tborgchartobjects o on o.id=u.department_id
                                               WHERE u.company_id={$db->escape($auth['company_id'])}
                                                    AND u.is_active='1' AND (u.email LIKE '%$search%' || u.first_name LIKE '%$search%' || u.last_name LIKE '%$search%' || u.position LIKE '%$search%')","numrows");

                        $res = array("sEcho"                        =>      intval($_POST['sEcho']),
                                         "iTotalRecords"                =>      $countRows,
                                         "iTotalDisplayRecords"         =>      $countRows,
                                         "aaData"                 =>      array());
                        foreach($companyUsers as $info){
                            $company_user_info = array();
                            // Actions
                            $actions = '<center><div class="icon-edit fl-margin-right fa fa-pencil cursor editUserListInfo" id="editUserListInfo_'.$info['id'].'" data-type="deactivate" data-user-id="'.$info['id'].'"></div>'.
                            '<div class="icon-trash fa fa-trash-o cursor deleteUserListInfo" id="deleteUser_'.$info['id'].'" data-type="deactivate" data-user-id="'.$info['id'].'"></div></center>';
                              
                            $checkbox = '<center><input type="checkbox" data-id="'.$info['id'].'" name="selectCheckBox[]" class="selectCheckBox css-checkbox" id="selectCheckBox_'.$info['id'].'"><label for="selectCheckBox_'.$info['id'].'" class="css-label"></label></center>';

                            //$company_user_info[] = $checkbox;
                            $company_user_info[] = '<div align="center">'.$userCompany->avatarPic("tbuser",$info['id'],"30","30","small","avatar");
                            $company_user_info[] = '<div class="fl-table-ellip" style="margin-left:0px">'.$info['email'].'</div>';
                            $company_user_info[] = '<div class="fl-table-ellip" style="margin-left:0px">'.$info['display_name'].'</div>';
                            $company_user_info[] = '<div class="fl-table-ellip" style="margin-left:0px">'.$info['first_name']. " " .$info['last_name'].'</div>';
                            $company_user_info[] = '<div class="fl-table-ellip" style="margin-left:0px">'.$info['position'].'</div>';
                            $company_user_info[] = '<div class="fl-table-ellip" style="margin-left:0px">'.$info['depName'].'</div>';
                            $company_user_info[] = '<div class="fl-table-ellip" style="margin-left:0px">'.$info['date_registered'].'</div>';
                            $company_user_info[] = $actions;
                            $res['aaData'][] = $company_user_info;
                            
                        }
                            
                        
                            $string = json_encode($res);
                            
                            echo $string;
                    break;
                case "companyUserDeactivated":
                    $res = "";
                    $out = "";
                    $fields_out = "";
                    $company_user_info = array();
                    $fields = array(); 
                    
                    //$companyUsers_fields = $db->query("SHOW COLUMNS FROM tbuser");
                    $company = $userCompany->getCompany($auth['company_id']);
                    $companyUsers = $db->query("SELECT *
                                               FROM tbuser WHERE company_id={$db->escape($auth['company_id'])}
                                                    AND is_active='0'
                                                ORDER BY date_registered DESC/* LIMIT 0, 10*/","array");
                    $countRows = $db->query("SELECT *
                                               FROM tbuser WHERE company_id={$db->escape($auth['company_id'])}
                                                    AND is_active='0'/* LIMIT 0, 10*/","numrows");
                    // Table fields
                        //foreach($companyUsers as $info){
                        //    $company_user_info[] = $info;
                        //}
                        
                        
                        foreach($companyUsers as $info){
                            // Actions
                            $actions = '<center><div class="icon-edit  cursor fa fa-pencil editUserListInfo" id="editUserListInfo_'.$info['id'].'" data-type="activate" data-user-id="'.$info['id'].'"></div>';
                            //'<div class="icon-trash cursor deleteUserListInfo" id="deleteUser_'.$info['id'].'" data-type="activate" data-user-id="'.$info['id'].'"></div></center>';
                              
                            $checkbox = '<center><input type="checkbox" data-id="'.$info['id'].'" name="selectCheckBox[]" class="selectCheckBox" ></center>';
                              
                            $company_user_info[] = array(
                                                        "userCheckbox"=>$checkbox,
                                                        "userID"=>$info['id'],
                                                        "image"=>$userCompany->avatarPic("tbuser",$info['id'],"30","30","small","avatar"),
                                                        "email"=>$info['email'],
                                                        "displayName"=>$info['display_name'],
                                                        "fullname"=>$info['first_name']. " " .$info['last_name'],
                                                        "position"=>$info['position'],
                                                        "department"=>$info['depName'],
                                                        "dateRegistered"=>$info['date_registered'],
                                                        "actions"=>$actions);
                            
                        }
                            $res = array(/*"sEcho"                        =>      "3",
                                         "iTotalRecords"                =>      $countRows,
                                         "iTotalDisplayRecords"         =>      $countRows,*/
                                         "company_user"                 =>      $company_user_info);
                        
                            $string = json_encode($res);
                            
                            echo functions::view_json_formatter($string);
                        break;
                case "companyUserDeactivatedDatatable":
                    $res = "";
                    $start = $_POST['iDisplayStart'];
                    $search = $_POST['search_value'];
                    $orderBy = " ORDER BY u.date_registered DESC";
                    if($_POST['column-sort']){
                      $orderBy = " ORDER BY ".$_POST['column-sort']." ".$_POST['column-sort-type'];
                    }
                    $company = $userCompany->getCompany($auth['company_id']);
                    $companyUsers = $db->query("SELECT u.*,o.department as depName
                                               FROM tbuser u
                                               LEFT JOIN tborgchartobjects o on o.id=u.department_id
                                               WHERE u.company_id={$db->escape($auth['company_id'])}
                                                    AND u.is_active='0' AND (u.email LIKE '%$search%' || u.first_name LIKE '%$search%' || u.last_name LIKE '%$search%' || u.position LIKE '%$search%')
                                                $orderBy LIMIT $start,10","array");
                    $countRows = $db->query("SELECT *
                                               FROM tbuser WHERE company_id={$db->escape($auth['company_id'])}
                                                    AND is_active='0' AND (email LIKE '%$search%' || first_name LIKE '%$search%' || last_name LIKE '%$search%' || position LIKE '%$search%')","numrows");
                   $res = array("sEcho"                        =>      intval($_POST['sEcho']),
                                         "iTotalRecords"                =>      $countRows,
                                         "iTotalDisplayRecords"         =>      $countRows,
                                         "aaData"                 =>      array());
                        
                        foreach($companyUsers as $info){
                            $company_user_info = array();
                            // Actions
                            $actions = '<center><div class="icon-edit  cursor fa fa-pencil editUserListInfo" id="editUserListInfo_'.$info['id'].'" data-type="activate" data-user-id="'.$info['id'].'"></div>';
                            //'<div class="icon-trash cursor deleteUserListInfo" id="deleteUser_'.$info['id'].'" data-type="activate" data-user-id="'.$info['id'].'"></div></center>';
                              
                            $checkbox = '<center><input type="checkbox" data-id="'.$info['id'].'" name="selectCheckBox[]" class="selectCheckBox css-checkbox" id="selectCheckBox2_'.$info['id'].'"><label for="selectCheckBox2_'.$info['id'].'" class="css-label"></label></center>';
                              
                            //$company_user_info[] = $checkbox;
                            $company_user_info[] = '<div align="center">'.$userCompany->avatarPic("tbuser",$info['id'],"30","30","small","avatar");
                            $company_user_info[] = '<div class="fl-table-ellip" style="margin-left:0px">'.$info['email'].'</div>';
                            $company_user_info[] = '<div class="fl-table-ellip" style="margin-left:0px">'.$info['display_name'].'</div>';
                            $company_user_info[] = '<div class="fl-table-ellip" style="margin-left:0px">'.$info['first_name']. " " .$info['last_name'].'</div>';
                            $company_user_info[] = '<div class="fl-table-ellip" style="margin-left:0px">'.$info['position'].'</div>';
                            $company_user_info[] = '<div class="fl-table-ellip" style="margin-left:0px">'.$info['depName'].'</div>';
                            $company_user_info[] = '<div class="fl-table-ellip" style="margin-left:0px">'.$info['date_registered'].'</div>';
                            $company_user_info[] = $actions;
                            $res['aaData'][] = $company_user_info;
                            
                        }
                            
                        
                            $string = json_encode($res);
                            
                            echo $string;
                        break;
                case "auditLogs":
                    $res = "";
                    $out = "";
                    $fields_out = "";
                    $audit_logs = array();
                    $fields = array(); 
                    
                    //$companyUsers_fields = $db->query("SHOW COLUMNS FROM tbuser");
                    $company = $userCompany->getCompany($auth['company_id']);
                    $getAuditLog = $db->query("SELECT *,a.id as auditID,a.user_id as uID,
                                              au.value as action,a.date as dateTime ,
                                              a.ip as ipAddress
                                              FROM tbaudit_logs a
                                              LEFT JOIN tbuser u on u.id=a.user_id
                                              LEFT JOIN tbaudit_action au on au.id
                                              WHERE a.is_active={$db->escape(1)}
                                              ORDER BY dateTime DESC LIMIT 3000","array");
                    // Table fields
                        //foreach($companyUsers as $info){
                        //    $company_user_info[] = $info;
                        //}
                        
                        
                        foreach($getAuditLog as $info){
                            // Actions
                            $actions = '<center><div class="icon-edit fa fa-pencil cursor editUserListInfo" id="editUserListInfo_'.$info['auditID'].'" data-type="deactivate" data-user-id="'.$info['auditID'].'"></div>'.
                            '<div class="icon-trash cursor fa fa-trash-o deleteUserListInfo" id="deleteUser_'.$info['auditID'].'" data-type="deactivate" data-user-id="'.$info['auditID'].'"></div></center>';
                              
                            $checkbox = '<center><input type="checkbox" data-id="'.$info['auditID'].'" name="selectCheckBox[]" class="selectCheckBox" ></center>';
                              
                            $audit_logs[] = array(
                                                        "userCheckbox"=>$checkbox,
                                                        "userID"=>$info['uID'],
                                                        "image"=>$userCompany->avatarPic("tbuser",$info['uID'],"30","30","small","avatar"),
                                                        "fullname"=>$info['first_name']. " " .$info['last_name'],
                                                        "action"=>$info['first_name']. " " .$info['last_name'] . " " .$info['action'],
                                                        "ip"=>$info['ipAddress'],
                                                        "dateRegistered"=>$info['dateTime'],
                                                        "tablename"=>$info['table_name'],
                                                        "type"=>$info['type'],
                                                        "actions"=>$actions);
                            
                        }
                            $res = array(/*"sEcho"                        =>      "3",
                                         "iTotalRecords"                =>      $countRows,
                                         "iTotalDisplayRecords"         =>      $countRows,*/
                                         "company_user"                 =>      $audit_logs);
                        
                            $string = json_encode($res);
                            
                            echo functions::view_json_formatter($string);
                    break;
            }
        }
        
?>